package com.jn.erp.wechat.common.api.controller.base;

import cn.hutool.core.util.StrUtil;
import com.github.pagehelper.PageInfo;
import com.jn.erp.common.constant.HttpStatus;
import com.jn.erp.common.exception.ServiceException;
import com.jn.erp.common.utils.ServletUtils;
import com.jn.erp.security.controller.BaseController;
import com.jn.erp.security.entity.LoginUser;
import com.jn.erp.security.service.TokenService;
import com.jn.erp.security.utils.SecurityUtils;
import com.jn.erp.wechat.core.WxTableData;
import com.jn.erp.wechat.core.WxTableDataInfo;
import com.jn.erp.wechat.common.api.entity.dto.WxUserDto;
import com.jn.erp.wechat.sys.entity.SysOrgWx;
import com.jn.erp.wechat.sys.service.SysOrgWxService;
import org.checkerframework.checker.units.qual.A;
import org.springframework.beans.factory.annotation.Autowired;

import java.util.List;

/**
 * <pre>
 *  移动端获取登录选定的机构用户信息
 * </pre>
 *
 * @author sunjiyong
 * @since 2024/8/27 7:58
 */
public class BaseWxAuthController extends BaseController {

    @Autowired
    private TokenService tokenService;

    @Autowired
    private SysOrgWxService sysOrgWxService;



    //不走网关信息，所以只能从header中获取登录信息
    public String loginDeptAuth() {
        String loginDept = ServletUtils.getRequest().getHeader("LoginDeptAuth");
        return loginDept;
    }

    //不走网关信息，所以只能从header中获取登录学员id信息
    public String LoginStudentAuth() {
        String loginStudent = ServletUtils.getRequest().getHeader("LoginStudentAuth");
        return loginStudent;
    }

    //用于获取教师登录后的基础关联信息
    public WxUserDto getStudentUser() {
        WxUserDto userDto = new WxUserDto();
        userDto.setUserId(SecurityUtils.getUserId());
        if (userDto.getUserId()==null) {
            throw new ServiceException("用户信息不全,内容获取失败");
        }
        return userDto;
    }

    public Long getPOrgId() {
        String orgId=loginDeptAuth();
        SysOrgWx byId = sysOrgWxService.getById(orgId);
        return byId.getPid();
    }

    //用于获取教师登录后的基础关联信息
    public WxUserDto getWxUser() {
        WxUserDto userDto = new WxUserDto();
        String orgId=loginDeptAuth();
        userDto.setOrgId(orgId);
        userDto.setUserId(SecurityUtils.getUserId());
        if (StrUtil.isBlank(userDto.getOrgId()) || userDto.getUserId() == null) {
            throw new ServiceException("用户信息不全,内容获取失败");
        }
        userDto.setOrgIdLong(Long.valueOf(orgId));
        return userDto;
    }


    /**
     * 响应请求分页数据
     */
    @SuppressWarnings({"rawtypes", "unchecked"})
    protected WxTableDataInfo getWxDataTable(List<?> list) {
        WxTableDataInfo rspData = new WxTableDataInfo();
        rspData.setCode(HttpStatus.SUCCESS);
        rspData.setMsg("查询成功");
        WxTableData rData = new WxTableData();
        rData.setRows(list);
        rData.setTotal(new PageInfo(list).getTotal());
        rspData.setData(rData);
        return rspData;
    }
}
